﻿using System;
using System.ComponentModel;
using System.Threading.Tasks;

namespace HobSoft.SoccerBet2014.Server.SiteManagerSchedule
{
    using Domain.DataService;
    using Domain.Models;
    public class BfManagerView : INotifyPropertyChanged, INotifyPropertyChanging
    {
        private readonly IBfManagerService bfRequestService;
        private readonly BFAccount innerAccount;
        public BfManagerView(BFAccount account, IBfManagerService bfRequestServiceImpl)
        {
            bfRequestService = bfRequestServiceImpl;
            Account = account.Account;
            AppKey = account.AppKey;
            Password = account.Password;
        }

        public async Task<bool> Signup()
        {
            var result = await bfRequestService.Signup(CertificateFile);
            if (result)
            {
                OnSignup = true;
                DisplayState = "登录成功";
                return true;
            }
            return false;
        }

        public async Task CheckMarketClose()
        {
            RaisePropertyChanged("LastCheckTime");
        }

        private string _displayState;
        public string DisplayState
        {
            get { return _displayState; }
            set
            {
                RaisePropertyChanging("DisplayState");
                if (_displayState != value)
                {
                    _displayState = value;
                    RaisePropertyChanged("DisplayState");
                }
            }
        }

        private bool _onSignup;
        public bool OnSignup
        {
            get { return _onSignup; }
            set
            {
                RaisePropertyChanging("OnSignup");
                if (_onSignup != value)
                {
                    _onSignup = value;
                    RaisePropertyChanged("OnSignup");
                }
            }
        }

        private string _account;
        public string Account
        {
            get { return _account; }
            protected set
            {
                RaisePropertyChanging("Account");
                if (_account != value)
                {
                    _account = value;
                    RaisePropertyChanged("Account");
                }
            }
        }

        private string _password;
        public string Password
        {
            get { return _password; }
            set
            {
                RaisePropertyChanging("Password");
                if (_password != value)
                {
                    _password = value;
                    RaisePropertyChanged("Password");
                }
            }
        }

        private string _session;
        public string Session
        {
            get { return _session; }
            set
            {
                RaisePropertyChanging("Session");
                if (_session != value)
                {
                    _session = value;
                    RaisePropertyChanged("Session");
                }
            }
        }

        private string _appKey;
        public string AppKey
        {
            get { return _appKey; }
            protected set
            {
                RaisePropertyChanging("AppKey");
                if (_appKey!=value)
                {
                    _appKey = value;
                    RaisePropertyChanged("AppKey");
                }
            }
        }

        private string _certificateFile;
        public string CertificateFile
        {
            get
            {
                return _certificateFile;
            }
            set
            {
                RaisePropertyChanging("CertificateFile");
                if (_certificateFile != value)
                {
                    _certificateFile = value;
                    RaisePropertyChanged("CertificateFile");
                }
            }
        }

        public DateTime? LastCheckTime
        {
            get
            {
                return bfRequestService.LoginInfo.LastRequestTime;
            }
        }

        protected void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        protected void RaisePropertyChanging(string propertyName)
        {
            if (PropertyChanging != null)
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public event PropertyChangingEventHandler PropertyChanging;
    }
}
